package com.api.controller;

import com.api.entity.*;
import com.api.log.Log;
import com.api.mapper.SysLogMapper;
import com.api.utils.ParamsUtils;
import com.api.utils.Result;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.api.entity.table.MenuRightTableDef.MENU_RIGHT;
import static com.api.entity.table.MenusTableDef.MENUS;
import static com.api.entity.table.RoleRightTableDef.ROLE_RIGHT;
import static com.api.entity.table.RolesTableDef.ROLES;
import static com.api.entity.table.SysDeptTableDef.SYS_DEPT;
import static com.api.entity.table.SysLogTableDef.SYS_LOG;
import static com.api.entity.table.UserRoleTableDef.USER_ROLE;

/**
 * @Author: yangsf
 * @CreateDate: 2024/10/22
 */

@Slf4j
@RestController
@RequestMapping("/system")
public class SystemController extends BaseController{


    @Autowired
    SysLogMapper logMapper;

    @Log("获取操作日志")
    @RequestMapping("/getLogs")
    public Result getLogs(@RequestBody Map<String,Object> params){
        QueryWrapper qw = new QueryWrapper();
        qw.select(SYS_LOG.ID,SYS_LOG.USER_NAME,SYS_LOG.LOGIN_NAME,SYS_LOG.OPER_IP,SYS_LOG.LOG_VALUE,SYS_LOG.CREATE_TIME,SYS_LOG.TAKE_TIME);
        if(params.containsKey("nameKeyword") && null != params.get("nameKeyword") && !StringUtils.isEmpty(params.get("nameKeyword"))){
            String nameKeyword = params.get("nameKeyword").toString();
            qw.and(SYS_LOG.LOGIN_NAME.like(nameKeyword).or(SYS_LOG.USER_NAME.like(nameKeyword)).or(SYS_LOG.LOG_VALUE.like(nameKeyword)));
        }
        if(params.containsKey("createTime") && null != params.get("createTime") && !StringUtils.isEmpty(params.get("createTime"))){
            String createTime = params.get("createTime").toString();
            qw.and(SYS_LOG.CREATE_TIME.gt(createTime + " 00:00:00"));
            qw.and(SYS_LOG.CREATE_TIME.lt(createTime + " 23:59:59"));
        }
        /*if(params.containsKey("startTime") && null != params.get("startTime") && !StringUtils.isEmpty(params.get("startTime"))){
            String startTime = params.get("startTime").toString();
            qw.and(ROLES.CREATE_TIME.gt(startTime));
        }
        if(params.containsKey("endTime") && null != params.get("endTime") && !StringUtils.isEmpty(params.get("endTime"))){
            String endTime = params.get("endTime").toString();
            qw.and(ROLES.CREATE_TIME.lt(endTime));
        }*/
        qw.orderBy(SYS_LOG.ID.desc());
        Page page = ParamsUtils.toPage(params);
        Page<SysLog> pages =logMapper.paginate(page,qw);
        return Result.ok(pages);
    }

    @Log("获取日志详情")
    @RequestMapping("/getLog")
    public Result getLog(Long id){
        if(null == id){
            return Result.err("未获取到日志信息");
        }
        SysLog sysLog = SysLog.create().setId(id).oneById();
        if(null==sysLog){
            return Result.err("未获取到日志信息");
        }
        return Result.ok(sysLog);
    }


    @Log("获取当前页面的权限")
    @RequestMapping("/getBtnList")
    public Result getBtnList(String path){
        //获取权限
        Integer roleType = getRoleType();
        List<String> list = null;
//        if(null != roleType && roleType.intValue()>20){
            Long userId = getUserId();
            List<Long> roleIds = UserRole.create()
                    .select(USER_ROLE.ROLE_ID)
                    .where(USER_ROLE.USER_ID.eq(userId))
                    .and(USER_ROLE.STATUS.eq(1))
                    .listAs(Long.class);
            List<Long> longs = MenuRight.create()
                    .select(MENU_RIGHT.MENU_ID)
                    .where(MENU_RIGHT.KIND.eq(2).and(MENU_RIGHT.RELL_ID.eq(userId)))
                    .or(MENU_RIGHT.KIND.eq(1).and(MENU_RIGHT.RELL_ID.in(roleIds)))
                    .and(MENU_RIGHT.STATUS.eq(1))
                    .listAs(Long.class);

            list = Menus.create()
                    .select(MENUS.CODE_NO)
                    .leftJoin(MENUS.as("pm")).on(MENUS.as("pm").ID.eq(MENUS.PARENT_ID))
                    .where(MENUS.as("pm").PATH.eq(path)).and(MENUS.MENU_TYPE.eq(3))
                    .and(MENUS.ID.in(longs))
                    .listAs(String.class);
//        }
//        else{
//            list = Menus.create()
//                .select(MENUS.CODE_NO)
//                .leftJoin(MENUS.as("pm")).on(MENUS.as("pm").ID.eq(MENUS.PARENT_ID))
//                .where(MENUS.as("pm").PATH.eq(path)).and(MENUS.MENU_TYPE.eq(3))
//                .listAs(String.class);
//        }

        return Result.ok(list);
    }



}
